package com.huilian.iotbox.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.dao.DisplayRackDao;
import com.huilian.iotbox.admin.service.DisplayRackItemService;
import com.huilian.iotbox.admin.service.DisplayRackService;
import com.huilian.iotbox.data.dto.DisplayRackDto;
import com.huilian.iotbox.data.dto.DisplayRackItemDto;
import com.huilian.iotbox.data.po.DisplayRack;
import com.huilian.iotbox.data.vo.DisplayRackItemVo;
import com.huilian.iotbox.data.vo.DisplayRackVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DisplayRackServiceImpl implements DisplayRackService {

    @Resource
    private DisplayRackDao displayRackDao;
    @Resource
    private DisplayRackItemService displayRackItemService;
    @Override
    public Integer update(DisplayRack displayRack) {
        return displayRackDao.update(displayRack);
    }

    @Override
    public DisplayRack findDisplayByDeviceId(Integer deviceId) {
        return displayRackDao.findDisplayByDeviceId(deviceId);
    }

    @Override
    public Integer updateIsDelete(DisplayRack displayRack) {
        return displayRackDao.updateIsDelete(displayRack);
    }

    @Override
    public PageInfo findDisplayByDeviceId(DisplayRackDto displayRackDto) {
        displayRackDto.setHierarchy(null);
        PageHelper.startPage(displayRackDto.getPageNum(), displayRackDto.getPageSize(), true);
        List<DisplayRackVo> list = displayRackDao.findDisplayListByDeviceId(displayRackDto);
        for (DisplayRackVo displayRackVo:list){
            DisplayRackItemDto displayRackItemDto = new DisplayRackItemDto();
            displayRackItemDto.setDisplayRackId(displayRackVo.getId());
            displayRackItemDto.setHierarchy(displayRackVo.getHierarchy());
            List<DisplayRackItemVo> displayRackVos = displayRackItemService.findDisplayByDeviceIdAndDisplayRackId(displayRackItemDto);
            displayRackVo.setDisplayRackItemVoList(displayRackVos);
        }
        return new PageInfo<>(list);
    }
}
